import {GeneratedType, Registry} from "@cosmjs/proto-signing";

require("dotenv").config();
const { DirectSecp256k1HdWallet } = require("@cosmjs/proto-signing")
const { SigningStargateClient, StargateClient } = require("@cosmjs/stargate")
const { stringToPath } = require("@cosmjs/crypto")
const { MsgDepositForBurn } = require("../generated/circle/cctp/v1/tx")

const config = require('../config');

export const cctpTypes: ReadonlyArray<[string, GeneratedType]> = [
    ["/circle.cctp.v1.MsgDepositForBurn", MsgDepositForBurn],
];

function createDefaultRegistry(): Registry {
    return new Registry(cctpTypes)
}
const getAccount1 = async ()=>{
    const mnemonic = config.MNEMONIC;
    
    const wallet1 = await DirectSecp256k1HdWallet.fromMnemonic(
        mnemonic,
        {
            hdPaths: [stringToPath("m/44'/118'/0'/0/0")],
            prefix: "noble"
        }
    );


    return wallet1;
}

const getAccount2 = async ()=>{
    const mnemonic = config.MNEMONIC;
   
    const wallet2 = await DirectSecp256k1HdWallet.fromMnemonic(
        mnemonic,
        {
            hdPaths: [stringToPath("m/44'/118'/0'/0/1")],
            prefix: "noble"
        }
    );

    const [account2] = await wallet2.getAccounts();
    console.log('account2: ', account2.address)

    const rpc = config.noble.url;
    const client = await StargateClient.connect(rpc);
    console.log("With client, chain id:", await client.getChainId(), ", height:", await client.getHeight())
    console.log(
        account2.address, "- balances:",
        await client.getAllBalances(account2.address)
    )

}

const main = async ()=>{
    const wallet1 = await getAccount1();
    const [account1] = await wallet1.getAccounts();
    console.log('account1: ', account1.address)

    const client = await SigningStargateClient.connectWithSigner(
        config.noble.url,
        wallet1,
        {
            registry: createDefaultRegistry()
        }
    );

    const rawMintRecipient = "0xdC484B57a5CC0563b42C2e3D37466b14186f501b" //goerli account2 
    const cleanedMintRecipient = rawMintRecipient.replace(/^0x/, '')
    const zeroesNeeded = 64 - cleanedMintRecipient.length;
    const mintRecipient = '0'.repeat(zeroesNeeded) + cleanedMintRecipient
    console.log("mintRecipient: ", mintRecipient)
    const buffer = Buffer.from(mintRecipient, "hex")
    const mintRecipientBytes = new Uint8Array(buffer);
    console.log("buffer: ", buffer)
    console.log("mintRecipientBytes: ", mintRecipientBytes)


    const msg = {
        typeUrl: "/circle.cctp.v1.MsgDepositForBurn",
        value: {
            from: account1.address,
            amount: "12345",
            destinationDomain: 0,
            mintRecipient: mintRecipientBytes,
            burnToken: "uusdc"
        }
    }

    const fee = {
        amount: [
            {
                denom: "uusdc",
                amount: "100",
            },
        ],
        gas: "200000",
    };
    const memo = "";

    const result = await client.signAndBroadcast(
        account1.address,
        [msg],
        fee,
        memo
    );
    console.log("Burned on Noble to to https://testnet.mintscan.io/noble-testnet/tx/" + result.transactionHash)
    console.log("Minting on Ethereum to https://goerli.etherscan.io/address/" + rawMintRecipient)
}
main();
//getAccount2();
/**
 * 
 * Burned on Noble to to https://testnet.mintscan.io/noble-testnet/tx/2667D1CE5D952C52A46408D67EA5271486C0B7E181AD1B8106EFC1833634B462
Minting on Ethereum to https://goerli.etherscan.io/address/0xdC484B57a5CC0563b42C2e3D37466b14186f501b

 */